<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="http://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<style>
    #app {
        width: 600px;
        margin: 10px auto;
    }

    .tb {
        border-collapse: collapse;
        width: 100%;
    }

    .tb th {
        background-color: #0094ff;
        color: white;
    }

    .tb td,
    .tb th {
        padding: 5px;
        border: 1px solid black;
        text-align: center;
    }

    .add {
        padding: 5px;
        border: 1px solid black;
        margin-bottom: 10px;
    }
</style>

<body>
    <div id="app">
        <div class="add">
            编号:<input type="text" v-model="code">
            品牌名称:<input type="text" v-model="productName">
            <br>
            产品图片地址:<input type="text" v-model="imgSrc">

            价格:<input type="text" v-model="price" @keydown.enter="add">
            <button @click="add">添加</button>
        </div>

        <div class="add">
            品牌名称:<input type="text" placeholder="请输入搜索条件">
        </div>

        <div>
            <table class="tb">
                <tr>
                    <th>
                        <input type="checkbox">
                    </th>
                    <th>产品图片</th>
                    <th>编号</th>
                    <th>品牌名称</th>
                    <th>价格</th>
                    <th>数量</th>
                    <th>操作</th>
                </tr>
                <tr v-for="(item,index) of cart" :key="item.id">
                    <td>
                        <input type="checkbox" name="" id="">
                    </td>
                    <td>
                        <img :src="item.imgSrc" height="50" width="50" alt="">
                    </td>
                    <td>{{item.code}}</td>
                    <td>{{item.productName}}</td>
                    <td>{{item.price}}</td>
                    <td>
                        <button>-</button>
                        &nbsp;
                        {{item.count}}
                        &nbsp;
                        <button>+</button>
                    </td>
                    <td>
                        <button @click="del(index)">删除</button>
                    </td>
                </tr>
                <!-- 动态生成内容tr -->

            </table>
            <div class="add" v-show="!cart.length">没有更多数据了</div>
        </div>
    </div>
</body>

</html>

<script>
    //需求
    //1.根据数据,将表格动态创建出来

    //2.实现添加功能

    //3.实现删除功能

    let data = [
        {
            id: 1,
            code: 1,
            imgSrc: "https://tse1-mm.cn.bing.net/th/id/R-C.fc48d5b3c65b7b79980ab09dfc2890d8?rik=jy07dRFBQAwjzw&riu=http%3a%2f%2f5b0988e595225.cdn.sohucs.com%2fimages%2f20190322%2fe2edee9d2ee847f0b564fcbadf356a46.jpeg&ehk=cKT9XpGfozyLcO5sia93t8gLeJurN9RM%2boqkkCk6a7Q%3d&risl=&pid=ImgRaw&r=0&sres=1&sresct=1",
            productName: "坤坤的背带裤",
            price: 9.9,
            count: 1
        },
        {
            id: 2,
            code: 2,
            imgSrc: "https://tse3-mm.cn.bing.net/th/id/OIP-C.U2zPWJK7Gus24zXyQWwi0AHaEG?pid=ImgDet&rs=1",
            productName: "品如的衣服",
            price: 19.9,
            count: 1
        },
        {
            id: 3,
            code: 3,
            imgSrc: "https://static1.paizi.com/uploadfile/2018/0710/20180710095354871.jpg",
            productName: "秀儿的秋裤",
            price: -9.9,
            count: 1
        },
    ]

    new Vue({
        el: "#app",
        data: {
            code: "",
            price: "",
            imgSrc: "",
            productName: "",
            cart: data
        },
        methods: {
            add() {
                // console.log('123')

                //输入内容为空是不能添加进来的

                if (!this.code || !this.price || !this.productName || !this.imgSrc) return

                let obj = {
                    id: Math.random().toString(32).substring(2),
                    code: this.code,
                    price: this.price,
                    imgSrc: this.imgSrc,
                    productName: this.productName,
                    count: 1
                }

                //添加操作
                this.cart.push(obj)
            },
            //删除逻辑
            del(i) {
                console.log(i)
                this.cart.splice(i,1)
            }
        }
    })
</script>